package com.controller;


import com.domain.Course;
import com.domain.CourseVO;
import com.domain.ResponseResult;
import com.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
    /*
     * 多条件课程列表查询
     * */
    @RequestMapping("/findCourseByCondition")
    public ResponseResult findCourseByCondition(@RequestBody CourseVO courseVO){
        List<Course> courses = courseService.findCourseByCondition(courseVO);
        System.out.println(courses);
        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", courses);
        return responseResult;
    }
    /*
    * 图片上传
    * */
    @RequestMapping("/courseUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws IOException {
        //判断接收到的是否为空
        if(file.isEmpty()){
            throw  new RuntimeException();
        }

        //获取项目的部署路径
        String realPath = request.getServletContext().getRealPath("/");
        String substring = realPath.substring(0, realPath.indexOf("ssm-web"));

        //获取上传文件的文件名
        String originalFilename = file.getOriginalFilename();

        //生成新文件名
        String newFileName = System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));

        //文件上传
        String uploadPath = substring + "upload\\";

        File filePath = new File(uploadPath, newFileName);


        if (!filePath.getParentFile().exists()){
            filePath.getParentFile().mkdirs();
            System.out.println("创建目录"+filePath);
        }

        file.transferTo(filePath);

        Map<String, String> map = new HashMap<>();
        map.put("fileName",newFileName);
        map.put("filePath","http://localhost:8080/upload/" + newFileName);

        ResponseResult responseResult = new ResponseResult(true, 200, "图片上传成功", map);
        return responseResult;
    }

    /*
    * 新增课程信息和讲师信息
    * */
    @RequestMapping("/saveOrUpdateCourse ")
    public ResponseResult saveOrUpdateCourse(@RequestBody CourseVO courseVO){
        if (courseVO.getId() == null){
            courseService.saveCourseOrTeacher(courseVO);
            return new ResponseResult(true,200,"新增成功",null);
        }else {
            courseService.updateCourseOrTeacher(courseVO);
            return new ResponseResult(true,200,"修改成功",null);
        }

    }

    /*
    * 回显课程信息
    * */
    @RequestMapping("/findCourseById")
    public ResponseResult findCourseById(int id){
        CourseVO courseVO = courseService.findCourseById(id);
        return  new ResponseResult(true,200,"回显成功",courseVO);
    }

    /*
    * 修改课程状态
    * */
    @RequestMapping("/updateCourseStatus")
    public ResponseResult updateCourseStatus(@RequestParam int id, @RequestParam int status){
        courseService.updateCourseStatus(id,status);
        Map<String, Integer> map = new HashMap<>();
        map.put("status",status);
        return new ResponseResult(true,200,"修改状态成功",map);
    }
}
